package com.littlebee.storysuper.utils;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
    public StringUtils() {
    }

    public static SpannableStringBuilder parse(String content, String pattern, int color) {
        SpannableStringBuilder spannableString = new SpannableStringBuilder(content);
        Pattern topicPattern = Pattern.compile(pattern);
        ForegroundColorSpan span = null;
        Matcher matcher = topicPattern.matcher(spannableString);

        while(matcher.find()) {
            span = new ForegroundColorSpan(color);
            spannableString.setSpan(span, matcher.start(), matcher.end(), 17);
        }

        return spannableString;
    }

    public static SpannableStringBuilder parse(String content, int start, int end, int color) {
        SpannableStringBuilder spannableString = new SpannableStringBuilder(content);
        ForegroundColorSpan span = new ForegroundColorSpan(color);
        spannableString.setSpan(span, start, end, 17);
        return spannableString;
    }

    public static SpannableStringBuilder parseLager(String content, String pattern) {
        SpannableStringBuilder spannableString = new SpannableStringBuilder(content);
        Pattern topicPattern = Pattern.compile(pattern);
        RelativeSizeSpan span = null;
        Matcher matcher = topicPattern.matcher(spannableString);

        while(matcher.find()) {
            span = new RelativeSizeSpan(1.5F);
            spannableString.setSpan(span, matcher.start(), matcher.end(), 17);
        }

        return spannableString;
    }

    public static String streamToString(InputStream is) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String s = "";
        StringBuilder sb = new StringBuilder();

        while((s = reader.readLine()) != null) {
            sb.append(new String(s.getBytes(), "UTF-8"));
        }

        return sb.toString();
    }

    public static InputStream stringToStream(String str) {
        return new ByteArrayInputStream(str.getBytes());
    }

    public static String formatPointStartAmount(String str) {
        return !TextUtils.isEmpty(str)?(str.startsWith(".")?"0" + str:str):str;
    }

    public static String formatAmount(String str, int decimals, boolean isSplit) {
        if(TextUtils.isEmpty(str) || "null".equalsIgnoreCase(str) || "0".equalsIgnoreCase(str)) {
            str = "0000000000";
        }

        if(str.length() < decimals) {
            str = "00000000" + str;
        }

        if(str.contains(",")) {
            str = str.replaceAll(",", "");
        }

        boolean negative = str.startsWith("-");
        if(negative) {
            str = str.replaceAll("-", "");
        }

        if(str.contains(".")) {
            str = str.replaceAll("\\.", "");
        }

        StringBuilder sb = new StringBuilder(str);
        if(sb.length() < decimals) {
            return str;
        } else {
            BigDecimal bigDecimal = new BigDecimal(sb.insert(sb.length() - decimals, ".").toString());
            sb.delete(0, sb.length());
            sb.append(bigDecimal.toPlainString());
            sb.reverse();
            StringBuilder sb2 = new StringBuilder();
            if(decimals > 0) {
                sb2.append(sb.subSequence(0, decimals + 1));
                sb.delete(0, decimals + 1);
            }

            if(isSplit) {
                for(int i = 0; i < (sb.length() % 3 == 0?sb.length() / 3:sb.length() / 3 + 1); ++i) {
                    sb2.append(sb.subSequence(i * 3, (i + 1) * 3 > sb.length()?sb.length():(i + 1) * 3)).append(",");
                }
            }

            sb2.deleteCharAt(sb2.length() - 1);
            return negative?sb2.reverse().insert(0, "-").toString():sb2.reverse().toString();
        }
    }

    public static String formatAmountY(String amt, int decimals, boolean bl) {
        int n = 0;
        if(TextUtils.isEmpty(amt) || "null".equals(amt)) {
            amt = "";
        }

        if(amt.contains(".")) {
            n = amt.split("\\.")[1].length();
        }

        while(n < decimals) {
            amt = amt + "0";
            ++n;
        }

        char c;
        for(c = 0; n > decimals; --n) {
            c = amt.charAt(amt.length() - 1);
            amt = amt.substring(0, amt.length() - 1);
        }

        if(c >= 53) {
            amt = amt + c;
            BigDecimal b = new BigDecimal(amt);
            b = b.setScale(decimals, 4);
            amt = b.toPlainString();
        }

        return formatAmount(amt, decimals, bl);
    }

    public static boolean validateMobileNumber(String mobileNumber) {
        if(mobileNumber.length() != 11) {
            return false;
        } else {
            String pMobileNumber = "^(1)[0-9]{10}$";
            boolean result = Pattern.matches(pMobileNumber, mobileNumber);
            return result;
        }
    }

    public static int getVersionCode(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 1).versionCode;
        } catch (NameNotFoundException var2) {
            return 1;
        }
    }

    public static String getVersionName(Context context) {
        try {
            return context.getPackageManager().getPackageInfo(context.getPackageName(), 1).versionName;
        } catch (NameNotFoundException var2) {
            return "1.0";
        }
    }
}

